//
//  WmcStyleButton.swift
//  RwaFishBubble
//
//  Created by Json on 2021/12/16.
//

import UIKit
import YPBedrock

class WmcStyleButton: UIButton {

    enum Style {
        case imageLeft
        case imageRight
        case imageTop
        case imageBottom
    }
    
    var style: Style = .imageLeft {
        didSet {
            setNeedsLayout()
        }
    }
    var space: Double = 0.0
    
    var imageHeight: CGFloat? {
        didSet {
            setNeedsLayout()
        }
    }
    var imageWidth: CGFloat? {
        didSet {
            setNeedsLayout()
        }
    }
    
    // MARK: Init
    init(){
        super.init(frame: .zero)
    }
    
    init(style: WmcStyleButton.Style, space: Double) {
        self.style = style
        self.space = space
        super.init(frame: .zero)
        imageView?.contentMode = .scaleAspectFit
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: Layout
    override func layoutSubviews() {
        super.layoutSubviews()
        // 按钮自身宽高
        let width = self.frame.width.yp.double
        let height = self.frame.height.yp.double
        // imageView宽高
        if let imageHeight = imageHeight {
            imageView?.frame.size.height = imageHeight
        }
        if let imageWidth = imageWidth {
            imageView?.frame.size.width = imageWidth
        }
        let imageW = imageView?.frame.width.yp.double ?? 0.0
        let imageH = imageView?.frame.height.yp.double ?? 0.0
        
        // titleLabel宽高
        var titleW = titleLabel?.frame.width.yp.double ?? 0.0
        let titleH = titleLabel?.frame.height.yp.double ?? 0.0
        // 判断title和image是否存在
        let isTitleEmpty = (currentTitle?.isEmpty ?? true)
        let isImageEmpty = currentImage == nil
        // 计算title和image之间的间距
        var imageTitleSpace = space
        if isTitleEmpty || isImageEmpty {
            imageTitleSpace = 0.0
        }
        // 计算内容宽高
        let contentW = imageW + imageTitleSpace + titleW
        let contentH = imageH + imageTitleSpace + titleH
        // 记录title和image的位置
        var titleX = 0.0
        var titleY = 0.0
        var imageX = 0.0
        var imageY = 0.0
        
        switch style {
        case .imageLeft:
            if contentHorizontalAlignment == .left {
                imageX = 0.0
            } else if contentHorizontalAlignment == .right {
                imageX = width - contentW
            } else {
                imageX = (width - contentW) * 0.5
            }
            imageY = (height - imageH) * 0.5
            titleX = imageX + imageW + imageTitleSpace
            titleY = (height - titleH) * 0.5
        case .imageRight:
            if contentHorizontalAlignment == .left {
                titleX = 0.0
            } else if contentHorizontalAlignment == .right {
                titleX = width - contentW
            } else {
                titleX = (width - contentW) * 0.5
            }
            titleY = (height - titleH) * 0.5
            imageX = titleX + titleW + imageTitleSpace
            imageY = (height - imageH) * 0.5
        case .imageTop:
            imageX = (width - imageW) * 0.5
            if contentVerticalAlignment == .top {
                imageY = 0.0
            } else if contentVerticalAlignment == .bottom {
                imageY = height - contentH
            } else {
                imageY = (height - contentH) * 0.5
            }
            titleW = width
            titleLabel?.yp.set(width: titleW)
            titleX = 0.0
            titleLabel?.textAlignment = .center
            titleY = imageY + imageH + imageTitleSpace
        case .imageBottom:
            titleX = 0.0
            titleLabel?.textAlignment = .center
            titleW = width
            titleLabel?.yp.set(width: titleW)

            if contentVerticalAlignment == .top {
                titleY = 0.0
            } else if contentVerticalAlignment == .bottom {
                titleY = height - contentH
            } else {
                titleY = (height - contentH) * 0.5
            }
            // 水平居中
            imageX = (width - imageW) * 0.5
            imageY = titleY + titleH + imageTitleSpace
        }
        updatePosition(x: titleX, y: titleY, for: titleLabel)
        updatePosition(x: imageX, y: imageY, for: imageView)
    }
    
    // MARK: Private method
    private func updatePosition(x: Double, y: Double, for view: UIView?) {
        view?.yp.set(x: x)
        view?.yp.set(y: y)
    }
    
    // MARK: Public method
    func fittedWidth(increment: Double = 1.0) -> Double {
        var elements = 0
        var titleW = 0.0
        if let title = currentTitle,
           title.isEmpty == false,
           let titleLabel = titleLabel {
            titleW = title.yp.textWidth(maxHeight: 0.0, font: titleLabel.font)
            elements += 1
        }
        var imageW = 0.0
        if let image = currentImage {
            imageW = image.size.width
            elements += 1
        }
        var spacing = 0.0
        if elements >= 2 {
            spacing = space
        }
        let width = titleW + spacing + imageW + increment
        return width
    }
    
    func widthToFit(increment: Double = 1.0) {
        yp.set(width: fittedWidth(increment: increment))
    }
    
    
    
}
